//
//  DataStatisticsViewController.m
//  CMVRFForIOS
//
//  Created by HailongHan on 15/4/19.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "DataStatisticsViewController.h"
#import "DataStatisticsFilterViewController.h"
#import "DataStatisticsData.h"
#import "DefaultStyleTableViewCell.h"

@interface DataStatisticsViewController ()

@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UITableView *tableView;

@end

@implementation DataStatisticsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self loadData];
    [self setupViews];
    
}

/**
 *  初始化数据
 */
- (void)initData {
    self.dataSource = [NSMutableArray new];
}

#pragma mark - 加载数据
/**
 *  加载视图所有的数据
 */
- (void)loadData {
    [self loadAllDataStatisticsData];
}

- (void)loadAllDataStatisticsData {
    [HttpClient getWithUrl:HTTP_GET_ALL_STATISTICS_DATA token:[[AccountManager manager]token] paramDic:nil resultClass:[DataStatisticsData class] success:^(DataStatisticsData *obj) {
        if (obj.getAllStatisticTemplateResult != nil && obj.getAllStatisticTemplateResult.count > 0) {
            [self.dataSource addObjectsFromArray:obj.getAllStatisticTemplateResult];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        CMLog(@"获取模板失败：%@", error);
    }];
}


#pragma mark - 初始化视图
- (void)setupViews{
    self.title = @"数据统计";
    
    //列表
    self.tableView = [[UITableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStylePlain];
    self.tableView.backgroundView = [[UIView alloc]init];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}


#pragma mark - tableView显示设置数据
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //报价
    static NSString *identifier2 = @"statisticsDataCell";
    DefaultStyleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier2];
    if (cell == nil) {
        cell = [[DefaultStyleTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier2];
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    DataStatisticsData *item = [self.dataSource objectAtIndex:indexPath.row];
    
    cell.textLabel.text = item.title;
    cell.detailTextLabel.text = item.comment;
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    DataStatisticsData *item = [self.dataSource objectAtIndex:indexPath.row];
    DataStatisticsFilterViewController *view = [DataStatisticsFilterViewController new];
    view.templeteId = item.guidId;
    view.templeteName = item.title;
    [self.navigationController pushViewController:view animated:YES];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CELL_HEIGHT;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
